﻿using BLL.Interface;
using Common.Extensions;
using DAL.Interface;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.DTO.Article;
using Model.DTO.Public.GetList;
using Model.DTO.Public.OperateId;
using Model.Entity.Type;

namespace BLL
{
    /// <summary>
    /// 文章类型业务逻辑层
    /// </summary>
    public class ArticleTypeBLL : IArticleTypeBLL
    {
        private readonly IBaseDAL<TB_ArticleType> _articleTypeDAL;
        public ArticleTypeBLL(IBaseDAL<TB_ArticleType> articleTypeDAL)
        {
            _articleTypeDAL = articleTypeDAL;
        }
        /// <summary>
        /// 添加文章类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> AddArticleType(AddArticleTypeDTO param)
        {
            try
            {
                #region 过滤
                var articleTypes = await _articleTypeDAL.GetEntityAllAsync();
                var existName = await articleTypes.Where(x => x.Name == param.Name).AnyAsync();
                if (existName)
                {
                    return ResponseMessage.CreateError("名称已存在");
                }
                var existCode = await articleTypes.Where(x => x.Code == param.Code).AnyAsync();
                if (existCode)
                {
                    return ResponseMessage.CreateError("代码已存在");
                }
                #endregion
                var articleType = new TB_ArticleType();
                param.CopyPropertiesTo(articleType);
                var result = await _articleTypeDAL.AddEntityAsync(articleType);
                return result ? ResponseMessage.CreateSuccess("添加成功") : ResponseMessage.CreateError("添加失败");
            }
            catch
            {
                return ResponseMessage.CreateError("添加失败");
            }
        }
        /// <summary>
        /// 删除文章类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> DeleteArticleType(OperateIdDTO_INT param)
        {
            try
            {
                if (param.Id > 0)
                {
                    param.Ids.Clear();
                    param.Ids.Add(param.Id);
                    param.Id = 0;
                    return await DeleteArticleType(param);
                }
                #region 过滤
                if (param.Ids.Count <= 0)
                {
                    return ResponseMessage.CreateError("Id无效");
                }
                #endregion
                using var tran = await _articleTypeDAL.BeginTransactionAsync();
                try
                {
                    foreach (var id in param.Ids)
                    {
                        await _articleTypeDAL.DeleteEntityAsync(id);
                    }
                    await tran.CommitAsync();
                    return ResponseMessage.CreateSuccess("删除成功");
                }
                catch
                {
                    await tran.RollbackAsync();
                    throw;
                }
            }
            catch
            {
                return ResponseMessage.CreateError("删除失败");
            }
        }
        /// <summary>
        /// 编辑文章类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> EditArticleType(EditArticleTypeDTO param)
        {
            try
            {
                #region 过滤
                var articleType = await _articleTypeDAL.GetEntityAllAsync(x => x.Id == param.Id).Result.FirstOrDefaultAsync();
                if (articleType == null)
                {
                    return ResponseMessage.CreateError("文章类型不存在");
                }
                var elseArticleTypes = await _articleTypeDAL.GetEntityAllAsync(x => x.Id != param.Id);
                var existName = await elseArticleTypes.Where(x => x.Name == param.Name).AnyAsync();
                if (existName)
                {
                    return ResponseMessage.CreateError("名称已存在");
                }
                var existCode = await elseArticleTypes.Where(x => x.Code == param.Code).AnyAsync();
                if (existCode)
                {
                    return ResponseMessage.CreateError("代码已存在");
                }
                #endregion
                param.CopyPropertiesTo(articleType);
                var result = await _articleTypeDAL.EditEntityAsync(articleType);
                return result ? ResponseMessage.CreateSuccess("编辑成功") : ResponseMessage.CreateError("编辑失败");
            }
            catch
            {
                return ResponseMessage.CreateError("编辑失败");
            }
        }
        /// <summary>
        /// 获取文章类型列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> GetArticleTypeList(GetListDTO param)
        {
            try
            {
                #region 参数校验
                if (param.Page <= 0)
                {
                    param.Page = 1;
                }
                if (param.Size <= 0)
                {
                    param.Size = 10;
                }
                #endregion
                var articleTypes = await _articleTypeDAL.GetEntityAllAsync(x => x.Name.Contains(param.Query ?? string.Empty));
                int total = await articleTypes.CountAsync();
                var list = await articleTypes.Skip((param.Page - 1) * param.Size).Take(param.Size).ToListAsync();
                return ResponseMessage.CreateSuccess("获取成功", new { total, list });
            }
            catch
            {
                return ResponseMessage.CreateError("获取失败");
            }
        }
    }
}
